#include "spi.h"
#include <stdio.h>


#define SPI_SCKMODE_CPOL 1
#define SPI_SCKMODE_CPHA 0

#define SPI_MOSI_GPIO_OFFSET 27

#define SPI_SCK_GPIO_OFFSET 29
/*SPI init*/
void SPI1_Init(void)
{	 
	QSPI_REG(SPI_REG_SCKMODE) &= ~((0x1 << SPI_SCKMODE_CPOL)|(0x1 << SPI_SCKMODE_CPHA));    // SCKMODE=00;
    QSPI_REG(SPI_REG_FCTRL) &= ~(0x1<< 0);  //disable flash xip mode
    QSPI_REG(SPI_REG_CSMODE) |= (0x1<< 1);  // hold mode
    QSPI_REG(SPI_REG_SCKDIV)=0x07;  // SCK config: f_sck = f_clk / (2*(div+1)) = f_clk / 16;
	SPI1_ReadWriteByte(0xff);   
}

/**
 * @brief Read/write Byte
 * TxData: Byte to be write
 * Return: Byte read.
 */
uint8_t SPI1_ReadWriteByte(uint8_t TxData)
{		
    QSPI_TXDATA(QSPI)=TxData;
    while(QSPI_STATUS(QSPI) & (1<<0));
    return QSPI_RXDATA(QSPI) & 0xff;			    
}
